package joodin.impl.widgets.internal;

import java.util.Iterator;

import joodin.impl.widgets.PopupCheckButtonImpl;

import org.jowidgets.common.image.IImageConstant;
import org.jowidgets.common.widgets.controler.IMenuListener;
import org.jowidgets.spi.widgets.IActionMenuItemSpi;
import org.jowidgets.spi.widgets.IMenuItemSpi;
import org.jowidgets.spi.widgets.ISelectableMenuItemSpi;
import org.jowidgets.spi.widgets.ISubMenuSpi;
import org.vaadin.hene.popupbutton.PopupButton;

import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;

public class PopupSubMenuImpl implements ISubMenuSpi{
	
	private PopupButton subMenu;
	
	public PopupSubMenuImpl(final ComponentContainer parent){
		subMenu = new PopupButton("");
//		parent.addComponent(subMenu);
	}
	
	@Override
	public void remove(int index) {
		Iterator<Component> it = subMenu.getComponentIterator();
		Component comp = null;
		for(int i = 0; it.hasNext() && i < index; i++){
			comp = it.next();
		}
 		if(comp == null){
 			return;
 		}
		subMenu.removeComponent(comp);		
	}

	@Override
	public IActionMenuItemSpi addActionItem(Integer index) {
		return new PopupButtonImpl(new Button());
	}

	@Override
	public ISelectableMenuItemSpi addCheckedItem(Integer index) {
		return new PopupCheckButtonImpl();
	}

	@Override
	public ISelectableMenuItemSpi addRadioItem(Integer index) {
		return new PopupCheckButtonImpl();
	}

	@Override
	public ISubMenuSpi addSubMenu(Integer index) {
		return new PopupSubMenuImpl(getUiReference());		 
	}

	@Override
	public IMenuItemSpi addSeparator(Integer index) {
		return new PopupSeparator(getUiReference());
	}

	@Override
	public PopupButton getUiReference() {
		return subMenu;
	}

	@Override
	public void setEnabled(boolean enabled) {
		getUiReference().setEnabled(enabled);
	}

	@Override
	public boolean isEnabled() {
		return getUiReference().isEnabled();
	}

	@Override
	public void addMenuListener(IMenuListener listener) {
		
	}

	@Override
	public void removeMenuListener(IMenuListener listener) {
				
	}

	@Override
	public void setText(String text) {
		getUiReference().setCaption(text);		
	}

	@Override
	public void setToolTipText(String text) {
		getUiReference().setDescription(text);		
	}

	@Override
	public void setIcon(IImageConstant icon) {
				
	}

	@Override
	public void setMnemonic(char mnemonic) {
				
	}

}
